package petstore.service;

import petstore.domain.Location;
import petstore.persistence.LocationDaoImpl;
import petstore.persistence.impl.LocationDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class LocationService extends HttpServlet {
    private LocationDao locationDao;

    public LocationService(int uid) {
        this.locationDao = new LocationDaoImpl(uid);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    public List<Location> getLocationList() {
        return locationDao.getLocation();
    }

    public int addLocation(String first, String second,String lidS)
    {
        if(lidS.equals("waiting"))
        {
            return locationDao.addLocation(first, second);
        }
        else
        {
            int lid = Integer.parseInt(lidS);
            for (Location location:getLocationList())
            {
                if(location.getLocationId() == lid)
                {
                    System.out.println("region:"+first);
                    System.out.println("detail:"+second);
                    locationDao.editLocation(lid,first,second);
                    return lid;
                }
            }
            return locationDao.addLocation(first,second);
        }
    }

    public void deleteLocation(int locationId){
        locationDao.removeLocation(locationId);
    }
}